package com.shycloud.mido.music.controller;

import com.qiniu.storage.model.FileInfo;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.music.domain.MisMusic;
import com.shycloud.mido.music.service.MisMusiOssService;
import com.shycloud.mido.music.service.MisMusicService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * @author nianhua.jiang
 * @description 曲目上传 controller
 * @date 2020/6/27
 */
@RestController
@RequestMapping("upload")
@AllArgsConstructor
@ApiIgnore
public class OssMusicController {

	/** 曲目上传模块 service */
	private MisMusiOssService ossMusicService;
	/** 七牛云 OSS */
	private MinioTemplate minioTemplate;
	/** 音乐模块 service */
	private MisMusicService musicService;

	/**
	 * 上传曲目资源
	 *
	 * @param files
	 * @param collectionId
	 * @param musicId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/30 15:21
	 */
	@PostMapping("uploadMusicFile")
	public R uploadMusicFile(@RequestParam("file") List<MultipartFile> files,
							 @RequestParam("collectionId") Integer collectionId,
							 @RequestParam("id") Integer musicId) {
		return new R(ossMusicService.uploadMusicFile(files, collectionId, musicId));
	}

	/**
	 * 上传伴奏资源
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/2 18:16
	 */
	@PostMapping("uploadKala")
	@ApiOperation("上传伴奏资源")
	public R uploadKala(@RequestParam("kalaFile") List<MultipartFile> files,
						@RequestParam("collectionId") Integer collectionId,
						@RequestParam("id") Integer musicId) {
		return new R(ossMusicService.uploadKala(files, collectionId, musicId));
	}


	/**
	 * 上传音视资源
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/2 18:16
	 */
	@PostMapping("uploadMp3AndMV")
	@ApiOperation("上传音视资源")
	public R uploadMp3AndMV(@RequestParam("mp3AndMvFile") List<MultipartFile> files,
							@RequestParam("collectionId") Integer collectionId,
							@RequestParam("id") Integer musicId) {
		return new R(ossMusicService.uploadMp3AndMV(files, collectionId, musicId));
	}

	/**
	 * 删除曲目资源
	 *
	 * @param key
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/2 17:58
	 */
	@ApiOperation("删除曲目资源")
	@GetMapping("delMusicFile")
	@SneakyThrows
	public R delMusicFile(String key) {
		return new R(minioTemplate.delete(key));
	}

	/**
	 * 删除曲目资源
	 *
	 * @param musicId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/2 17:58
	 */
	@ApiOperation("删除曲目资源")
	@GetMapping("delMusicFileAll")
	@SneakyThrows
	public R delMusicFileAll(Integer musicId) {

		MisMusic music = musicService.getById(musicId);
		FileInfo[] allObjectsByPrefix = null;
		if (null != music.getOssFile() && !music.getOssFile().isEmpty()) {
			allObjectsByPrefix = minioTemplate.getAllObjectsByPrefix("mido-songbook-sourcefile", music.getOssFile(), true);
		}
		for (FileInfo objectsByPrefix : allObjectsByPrefix) {
			minioTemplate.delete(objectsByPrefix.key);
		}
		return new R(true);
	}


	/**
	 * 曲目批量上传
	 *
	 * @param files
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/14 14:07
	 */
	@PostMapping("uploadBatchMusic")
	@ApiOperation("批量上传")
	public R uploadBatchMusic(@RequestParam("files") List<MultipartFile> files, Integer collectionId) {
		return new R(ossMusicService.uploadBatchMusic(files, collectionId));
	}


	/**
	 * 伴奏批量上传
	 *
	 * @param files
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/14 14:07
	 */
	@PostMapping("uploadBatchKala")
	@ApiOperation("伴奏批量上传")
	public R uploadBatchKala(@RequestParam("files") List<MultipartFile> files, Integer collectionId) {
		return new R(ossMusicService.uploadBatchKala(files, collectionId));
	}

	/**
	 * 音频批量上传
	 *
	 * @param files
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/14 14:07
	 */
	@PostMapping("uploadBatchMP3")
	@ApiOperation("音频批量上传")
	public R uploadBatchMP3(@RequestParam("files") List<MultipartFile> files, Integer collectionId) {
		return new R(ossMusicService.uploadBatchMP3(files, collectionId));
	}

	/**
	 * mv文件批量上传
	 *
	 * @param files
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/14 14:07
	 */
	@PostMapping("uploadBatchMv")
	@ApiOperation("mv文件批量上传")
	public R uploadBatchMv(@RequestParam("files") List<MultipartFile> files, Integer collectionId) {
		return new R(ossMusicService.uploadBatchMv(files, collectionId));
	}

	/**
	 * mv文件批量上传
	 *
	 * @param updateBatchId
	 * @param collectionId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/14 14:07
	 */
	@GetMapping("checkBatchUploadResult")
	@ApiOperation("查看mp4文件上传结果")
//	public R checkBatchUploadResult(@RequestParam("updateBatchId") String updateBatchId,
//									@RequestParam("collectionId") Integer collectionId) {
	public R checkBatchUploadResult(String updateBatchId, Integer collectionId) {
		return new R(ossMusicService.checkBatchMvResult(updateBatchId, collectionId));
	}

}
